package com.hankol.winecellar.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.baselibrary.utils.GlideUtils;
import com.baselibrary.view.RoundCornerImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hankol.winecellar.R;
import com.hankol.winecellar.bean.WineryCoverBean;
import com.baselibrary.view.GlideRoundTransform;

import java.util.ArrayList;
import java.util.List;

/**
 * 酒窖编辑--上传图片、视频
 *
 * @author: merbng
 * @time: 2022/7/6 12:19
 * @description:
 */
public class EditWineryUploadImageVideoAdapter extends RecyclerView.Adapter<EditWineryUploadImageVideoAdapter.ImageVideoHolder> {
    private Context mContext;
    private List<WineryCoverBean> mList = new ArrayList<>();
    private LayoutInflater mInflater;
    private OnItemClickListener mOnItemClickListener;
    private ItemTouchHelper mItemHelper;

    public EditWineryUploadImageVideoAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public List<WineryCoverBean> getListData() {
        return mList;
    }

    public void setData(List<WineryCoverBean> list) {
        this.mList = list;
    }

    public void addData(List<WineryCoverBean> list) {
        this.mList.addAll(list);
    }

    public void setOnClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setItemHelper(ItemTouchHelper mItemHelper) {
        this.mItemHelper = mItemHelper;
    }

    @NonNull
    @Override
    public EditWineryUploadImageVideoAdapter.ImageVideoHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new EditWineryUploadImageVideoAdapter.ImageVideoHolder(mInflater.inflate(R.layout.item_upload_imagevideo, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull EditWineryUploadImageVideoAdapter.ImageVideoHolder imageVideoHolder, int position) {
        if (position < mList.size()) {
            WineryCoverBean coverBean = mList.get(position);
            //代表+号之前的需要正常显示图片
            imageVideoHolder.mIvCoversType.setVisibility(coverBean.getType() == 1 ? View.VISIBLE : View.GONE);
            String picUrl = coverBean.getType() == 0 ? coverBean.getCoverUrl() : coverBean.getVideoUrl(); //图片路径
//            RequestOptions userAvatarOptions = new RequestOptions().signature(new ObjectKey(System.currentTimeMillis()));
            GlideUtils.loadImg_101(mContext,picUrl,imageVideoHolder.mIvFriendlistHead);
//            Glide.with(mContext).applyDefaultRequestOptions(userAvatarOptions).load(picUrl).placeholder(R.mipmap.ic_hankol_101).into(imageVideoHolder.mIvFriendlistHead);
            if (mList.size() >= 1) {
                imageVideoHolder.mIvDelete.setVisibility(View.VISIBLE);
            } else {
                imageVideoHolder.mIvDelete.setVisibility(View.GONE);
            }
            imageVideoHolder.listener(imageVideoHolder, position);
            if (coverBean.getType() != 1) {
                imageVideoHolder.setDragListener(imageVideoHolder);
            } else {
                imageVideoHolder.removeDragListener(imageVideoHolder);
            }
        } else {
            //添加按钮
            imageVideoHolder.mIvCoversType.setVisibility(View.GONE);
            Glide.with(mContext)
                    .load(R.drawable.ic_upload_temp_3)
                    .centerCrop()
                    .placeholder(R.drawable.ic_hankol_101)
                    .error(R.drawable.ic_hankol_101)
                    .transform(new GlideRoundTransform(mContext, 8))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(imageVideoHolder.mIvFriendlistHead);
            imageVideoHolder.mIvDelete.setVisibility(View.GONE);
            imageVideoHolder.mIvFriendlistHead.setOnLongClickListener(view -> {
                //拖拽Item 这里不添加任何点击事件。。但是不实现长按监听，则默认执行拖拽方法，所以这里面什么都不做
                return false;
            });
            imageVideoHolder.mIvFriendlistHead.setOnClickListener(view -> {
                if (mOnItemClickListener != null)
                    mOnItemClickListener.onAddItemClick();
            });
        }
    }

    @Override
    public int getItemCount() {
        int count = mList == null || mList.size() == 0 ? 1 : mList.size() == 9 ? mList.size() : mList.size() + 1;
        return count;
    }

    public class ImageVideoHolder extends RecyclerView.ViewHolder {
        private RoundCornerImageView mIvFriendlistHead;
        private ImageView mIvCoversType;
        private LinearLayout mIvDelete;

        public ImageVideoHolder(@NonNull View itemView) {
            super(itemView);
            mIvFriendlistHead = itemView.findViewById(R.id.pic_iv);
            mIvDelete = itemView.findViewById(R.id.iv_delete);
            mIvCoversType = itemView.findViewById(R.id.iv_covers_type);
        }

        private void listener(@NonNull ImageVideoHolder imageVideoHolder, int position) {
            imageVideoHolder.mIvFriendlistHead.setOnClickListener(view -> {
                if (mOnItemClickListener != null)
                    mOnItemClickListener.onItemClick(position, mList);
            });
            imageVideoHolder.mIvFriendlistHead.setOnLongClickListener(view -> {
                if (mItemHelper != null)
                    mItemHelper.startDrag(imageVideoHolder);
                return false;
            });
            imageVideoHolder.mIvDelete.setOnClickListener(view -> {
                if (mOnItemClickListener != null)
                    mOnItemClickListener.onDeleteItemClick(position, mList);
            });
        }

        private void setDragListener(@NonNull ImageVideoHolder imageVideoHolder) {
            imageVideoHolder.mIvFriendlistHead.setOnLongClickListener(view -> {
                if (mItemHelper != null)
                    mItemHelper.startDrag(imageVideoHolder);
                return false;
            });
        }

        private void removeDragListener(@NonNull ImageVideoHolder imageVideoHolder) {
            imageVideoHolder.mIvFriendlistHead.setOnLongClickListener(view -> {
                return false;
            });
        }
    }


    public interface OnItemClickListener {
        void onItemClick(int pos, List<WineryCoverBean> list);

        void onAddItemClick();

        void onDeleteItemClick(int pos, List<WineryCoverBean> list);

        void onVideoCoversClick();
    }
}

